package com.inxedu.os.edu.controller.system;

import com.google.gson.Gson;
import com.inxedu.os.common.controller.BaseController;
import com.inxedu.os.edu.pojo.SysFunction;
import com.inxedu.os.edu.pojo.SysRole;

import com.inxedu.os.edu.service.system.SysRoleService;
import com.inxedu.os.edu.service.system.SystemFounctionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/sysrole")
@Api(value = "角色管理页面Controller",tags = "角色管理")
public class SysRoleController  {

    @InitBinder({"sysRole"})
    public void initBinderSysFunction(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("sysRole.");
    }
    @Autowired
    private SysRoleService sysRoleServiceimpl;

    @Autowired
    private SystemFounctionService systemFounctionServiceImpl;

    public Map<String, Object> setJson(boolean success, String message, Object entity) {
        Map<String, Object> json = new HashMap();
        json.put("success", success);
        json.put("message", message);
        json.put("entity", entity);
        return json;
    }

    /**
     * 显示所有权限
     * */
    @ApiOperation("显示角色权限")

    @RequestMapping(value = "/showroleList",method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView showRoleList() {
        Gson gson = new Gson();
        ModelAndView model = new ModelAndView();
        //后台用户角色list页面
        model.setViewName("view/inxedu/admin/system/sys-role-page");
        //查询所有的角色
        List<SysRole> roleList = sysRoleServiceimpl.queryAllRoleList();
        model.addObject("roleList", roleList);
        //查询所有的权限
        List<SysFunction> functionList = systemFounctionServiceImpl.findSystrmFunctionAll();
        model.addObject("functionList", gson.toJson(functionList));
        return model;
    }

    /**
     * 根据选中的ID查询角色权限
     *
     * */
    @ApiOperation("根据角色id显示权限")
    @ApiImplicitParam(name = "roleId" ,value = "接收一个SroleId！",dataType="Integer")

    @RequestMapping(value = "/changesrole/{roleId}",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> selectRoleById(@PathVariable("roleId") Integer roleId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map= setJson(false, null, null);
        if(roleId>0){
            List<Integer> sysRole = sysRoleServiceimpl.selectRoleById(roleId);
            map = this.setJson(true, null, sysRole);
        }
        return map;
    }

    /**
     * 根据type创建、修改
     * 根据type 的值决定功能
     * @param
     * @return
     */

    @ApiOperation("修改或创建角色权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "接收一个type！"),
            @ApiImplicitParam(name = "roleName", value = "接收一个roleName！",dataType = "String")
    })
    @RequestMapping(value = "/editrole/{type}",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateRoleByType(@PathVariable("type") Integer type, @ModelAttribute("sysRole") SysRole sysRole){
        Map<String, Object> map = new HashMap<String, Object>();


        //type=1创建 ，type=2修改
        if (type==1){
            int i=sysRoleServiceimpl.insertSysRoleByRoleName(sysRole);
            if (i>0){
                sysRole=sysRoleServiceimpl.findSysRoleByRoleName(sysRole);
                map=this.setJson(true,"添加成功!",sysRole);
                return map;
            }else {

                map=this.setJson(false,"添加失败！",null);
                return map;
            }
        }
        //修改
        else if (type==2){
            int i=sysRoleServiceimpl.updateSysRoleName(sysRole);
            if (i>0){
                sysRole=sysRoleServiceimpl.findSysRoleByRoleName(sysRole);
                map=this.setJson(true,"修改成功!",sysRole);
                return map;
            }else {

                map=this.setJson(false,"修改失败！",null);
                return map;
            }

        }

    return map;
    }


    /**
     * 保存权限
     * @param roleId
     * @param functionIds
     * @return
     */

    @ApiOperation("保存权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "接收一个roleId！"),
            @ApiImplicitParam(name = "functionIds", value = "接收一个functionIds字符串！",dataType = "String")
    })
    @RequestMapping(value = "/saveroelfunction/{roleId}",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveSysRoleByRoleId(@PathVariable("roleId") Integer roleId, @RequestParam String functionIds){
        Map<String, Object> map = new HashMap<String, Object>();
        int i=sysRoleServiceimpl.saveSysRoleByRoleId(roleId,functionIds);
        if(i>0){
            map=this.setJson(true,"保存成功！",null);

        }else {
            map=this.setJson(false,"保存失败！",null);
        }

        return map;

    }


    @ApiOperation("删除角色")
    @ApiImplicitParam(name = "roleId" ,value = "roleId！",dataType="Integer")

    @RequestMapping(value = "/deleterole/{roleId}",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteSysRoleByRoleId(@PathVariable("roleId") Integer roleId){
        Map<String, Object> map = new HashMap<String, Object>();
        int i=sysRoleServiceimpl.deleteSysRoleByRoleId(roleId);
        if(i>0){
            map=this.setJson(true,"删除角色成功！",null);

        }else {
            map=this.setJson(false,"删除角色失败！",null);
        }
        return map;
    }




}
